/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.qframe.client.render;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import java.util.ArrayList;
import org.qframe.client.error.TemplateConfigError;
import org.qframe.client.gwtcustom.QuickletHTMLPanel;
import org.qframe.client.quicklet.beans.Quicklet;
import org.qframe.client.quicklet.beans.RenderedQuicklet;
import org.qframe.client.quicklet.beans.RenderedSlot;
import org.qframe.client.quicklet.beans.Slot;
import org.qframe.client.util.Log;

/**
 *
 * @author gprasad
 */
public class DomUtils {

    public static final String TOPMOST_SLOT_PREFIX = "QLSLTIDS";
    private static final String TAG_SLOT = "slot";
    private static final String TAG_SLOT_ID = "id";
    private static final String TAG_SLOT_QUICKLET = "quicklet";
    private static final String TAG_QUICKLET = "quicklet";
    private static final String TAG_QUICKLET_ID = "id";
    public static final String SLOT_ID_PARTITION = ":##:";
    private static String mainQlId = null;

    public static ArrayList<Slot> getSlots(Panel containerPanel) {

        NodeList<Element> slotElements = containerPanel.getElement().getElementsByTagName(TAG_SLOT);
        ArrayList<Slot> slots = new ArrayList<Slot>();
        for (int i = 0; i < slotElements.getLength(); i++) {
            Element slotDomElement = slotElements.getItem(i);
            String renderedSlotId = slotDomElement.getAttribute(TAG_SLOT_ID);
            String[] tokens = renderedSlotId.split(SLOT_ID_PARTITION);
            String slotId = tokens[tokens.length - 1];

            String defaultQuickletId = slotDomElement.getAttribute(TAG_SLOT_QUICKLET);

            Quicklet defaultQuickletForSlot = new Quicklet(defaultQuickletId);
            Slot slot = new Slot(slotId, defaultQuickletForSlot);
            slots.add(slot);

        }
        return slots;
    }

    public static RenderedQuicklet renderWelcomeQuicklet() {
        Element welcomeElement = getWelcomeElement();
        String welcomeQuickletId = welcomeElement.getAttribute(TAG_QUICKLET_ID);
        mainQlId = welcomeQuickletId;
        if (welcomeQuickletId == null || welcomeQuickletId.trim().equals("")) {
            throw new TemplateConfigError("You no id defined for welcome page quicklet " + welcomeQuickletId);
        }
        setTagIds(welcomeQuickletId, welcomeElement.getElementsByTagName(TAG_SLOT), null);
        String innerQuickletHtml = welcomeElement.getInnerHTML();
        welcomeElement.setInnerHTML("");
        QuickletHTMLPanel generatedHTMLPanel = new QuickletHTMLPanel(innerQuickletHtml);
        RootPanel.get(welcomeQuickletId).add(generatedHTMLPanel);
        RenderedQuicklet welcomeQuicklet = new RenderedQuicklet(welcomeQuickletId, generatedHTMLPanel, null);
        return welcomeQuicklet;
    }

    private static Element getWelcomeElement() throws TemplateConfigError {
        NodeList<Element> welcomeQlElements = RootPanel.getBodyElement().getElementsByTagName(TAG_QUICKLET);
        if (welcomeQlElements == null || welcomeQlElements.getLength() != 1) {
            throw new TemplateConfigError("You can only have one quicklet in the welcome page. You have :" + welcomeQlElements);
        }
        Element welcomeElement = welcomeQlElements.getItem(0);
        return welcomeElement;
    }

    public static void dumpHtml() {
        Element element = RootPanel.get(mainQlId).getElement();
        Log.out("HTML >>>>> ");
        Log.out("Content : \n " + element.getInnerHTML() + "\n ------finished----- ");

    }

    static QuickletHTMLPanel createQuickletHtmlPanel(Quicklet quickletObj, String html, RenderedSlot renderedInSlot) {

        HTMLPanel panel = new HTMLPanel(html);
        NodeList<Element> tags = panel.getElement().getElementsByTagName(TAG_SLOT);
        //RenderedSlot renderedSlot = (renderingPayLoad.containerQicklet == null) ? null : renderingPayLoad.containerQicklet.getContainerSlot();
        setTagIds(quickletObj.getQuickletId(), tags, renderedInSlot);
        html = panel.getElement().getInnerHTML();
        return new QuickletHTMLPanel(html);

    }

    private static void setTagIds(String quickletId, NodeList<Element> tags, RenderedSlot renderedInSlot) {

        String containerRenderedSlotId = (renderedInSlot == null) ? TOPMOST_SLOT_PREFIX
                : renderedInSlot.getRenderedSlotId();
        ArrayList<String> duplicateChecker = new ArrayList<String>();
        for (int i = 0; i < tags.getLength(); i++) {
            Element element = tags.getItem(i);
            if (duplicateChecker.contains(element.getId())) {
                throw new TemplateConfigError(" Slot id :" + element.getId() + " -> duplicate in Quicklet : " + quickletId + ". SlotIds have to be unique in a quicklet.");
            }
            duplicateChecker.add(element.getId());
            String id = containerRenderedSlotId + SLOT_ID_PARTITION + element.getId();
            element.setId(id);
        }
    }
}
